﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using System.Data;
using System.Windows.Forms;
using UniMana.Data;

namespace UniMana.Win.Personnel
{
    public partial class FrmAttendance_RptMonth : DevExpress.XtraReports.UI.XtraReport
    {
        public FrmAttendance_RptMonth(DataTable tblSource, string month, string year, string dept)
        {
            InitializeComponent();
            this.DataSource = tblSource;
            this.LoadReport();
            xrMonth.Text = month;
            xrYear.Text = year;

            int iMonthTo = int.Parse(month);
            int iMonthFrom = iMonthTo-1>0?iMonthTo-1:12;
            int iYearTo = int.Parse(year);
            int iYearFrom = iMonthFrom == 12 ? iYearTo - 1 : iYearTo;

            xrlFromTo.Text = "Từ ngày 26/" + iMonthFrom+"/"+iYearFrom+" đến ngày 25/"+iMonthTo+"/"+iYearTo;

            #region [Ẩn cột dư]
            int days = DateTime.DaysInMonth(iYearFrom,iMonthFrom);
            if (days == 28)
            {
                this.xrTableRow1.Cells.Remove(xr29);
                this.xrTableRow1.Cells.Remove(xr30);
                this.xrTableRow1.Cells.Remove(xr31);

                this.xrTableRow2.Cells.Remove(xrt29);
                this.xrTableRow2.Cells.Remove(xrt30);
                this.xrTableRow2.Cells.Remove(xrt31);
            }
            else if (days == 29)
            {
                this.xrTableRow1.Cells.Remove(xr30);
                this.xrTableRow1.Cells.Remove(xr31);

                this.xrTableRow2.Cells.Remove(xrt30);
                this.xrTableRow2.Cells.Remove(xrt31);
            }
            else if (days == 30)
            {
                this.xrTableRow1.Cells.Remove(xr31);
                this.xrTableRow2.Cells.Remove(xrt31);
            }
            #endregion

            #region [Tô màu các cột ngày chủ nhật]
            foreach (DevExpress.XtraReports.UI.XRTableCell cell in xrTableRow2.Cells)
            {
                if (cell.Name.Contains("NB")) continue;
                else
                {
                    try
                    {
                        int day = int.Parse(cell.Name.Substring(3));
                        if (day > 25)
                        {
                            if (new DateTime(iYearFrom, iMonthFrom, day).DayOfWeek.ToString() == "Sunday")
                            {
                                cell.BackColor = System.Drawing.Color.Salmon;
                            }
                        }
                        else
                        {
                            if (new DateTime(iYearTo, iMonthTo, day).DayOfWeek.ToString() == "Sunday")
                            {
                                cell.BackColor = System.Drawing.Color.Salmon;
                            }
                        }
                    }
                    catch { }
                }
            }
            #endregion 
            xrDept.Text = dept.ToUpper();
        }

        public void LoadReport()
        {
           
            NBxrtName.DataBindings.Add("Text", null, "Name");
            NBxrtLastName.DataBindings.Add("Text", null, "LastName");

            xrt26.DataBindings.Add("Text", null, "D26");
            xrt27.DataBindings.Add("Text", null, "D27");
            xrt28.DataBindings.Add("Text", null, "D28");
            try
            {
                xrt29.DataBindings.Add("Text", null, "D29");
            }
            catch {  }

            try
            {
                xrt30.DataBindings.Add("Text", null, "D30");
            }
            catch {  }

            try
            {
                xrt31.DataBindings.Add("Text", null, "D31");
            }
            catch { }
            xrt1.DataBindings.Add("Text", null, "D1");
            xrt2.DataBindings.Add("Text", null, "D2");
            xrt3.DataBindings.Add("Text", null, "D3");
            xrt4.DataBindings.Add("Text", null, "D4");
            xrt5.DataBindings.Add("Text", null, "D5");
            xrt6.DataBindings.Add("Text", null, "D6");
            xrt7.DataBindings.Add("Text", null, "D7");
            xrt8.DataBindings.Add("Text", null, "D8");
            xrt9.DataBindings.Add("Text", null, "D9");
            xrt10.DataBindings.Add("Text", null, "D10");
            xrt11.DataBindings.Add("Text", null, "D11");
            xrt12.DataBindings.Add("Text", null, "D12");
            xrt13.DataBindings.Add("Text", null, "D13");
            xrt14.DataBindings.Add("Text", null, "D14");
            xrt15.DataBindings.Add("Text", null, "D15");
            xrt16.DataBindings.Add("Text", null, "D16");
            xrt17.DataBindings.Add("Text", null, "D17");
            xrt18.DataBindings.Add("Text", null, "D18");
            xrt19.DataBindings.Add("Text", null, "D19");
            xrt20.DataBindings.Add("Text", null, "D20");
            xrt21.DataBindings.Add("Text", null, "D21");
            xrt22.DataBindings.Add("Text", null, "D22");
            xrt23.DataBindings.Add("Text", null, "D23");
            xrt24.DataBindings.Add("Text", null, "D24");
            xrt25.DataBindings.Add("Text", null, "D25");

            NBxrtAbsent.DataBindings.Add("Text", null, "AbsentDay");
            NBxrtWorks.DataBindings.Add("Text", null, "WorkDay");

            DBMain data = new DBMain();
            DateTime d = data.m_DCSave.GetSystemDate();
            string day = d.Day >= 10 ? d.Day.ToString() : "0" + d.Day;
            string month = d.Month >= 10 ? d.Month.ToString() : "0" + d.Month;

            xrtFooter.Text = "Tân An, ngày " + day + " ,tháng " + month + " , năm " + d.Year;


        }

        private void RptAttendance_ParametersRequestSubmit(object sender, DevExpress.XtraReports.Parameters.ParametersRequestEventArgs e)
        {
            xrlAllower.Text = parameter1.Value.ToString();
            xrlMaker.Text = SysGlobalVar.g_sta_sysUserLogin.Properties.FullName;
            
        }

    }
}
